Flutter Navigator _RouteEntry
The _RouteEntry
class in Flutter, typically an internal class indicated by the leading underscore in its name, serves as a crucial component in the Flutter navigation system. It's not part of the public API, but understanding its role can provide deeper insights into how navigation works in Flutter.
Core Functionality and Role
-
Representation of Route in Navigator's Stack:
_RouteEntry
acts as a container or wrapper for aRoute
within theNavigator
's internal stack. Each_RouteEntry
corresponds to aRoute
that has been pushed onto the Navigator. -
Lifecycle State Tracking: It tracks the lifecycle state of the
Route
it contains. This includes states like whether the route is currently being pushed, popped, or is idle. -
Transition Management:
_RouteEntry
plays a key role in managing the transitions and animations as theRoute
enters or exits the screen.- It coordinates the animations and ensures they are in sync with the route's current state.
_RouteEntry
extends _RouteEntry class.
-
Result Handling: It also handles the result of a route when it's popped. This is important for scenarios where routes return data upon being popped.
-
Overlay Management: Since routes often have associated
Overlay
entries (for displaying the route's UI),_RouteEntry
helps manage theseOverlay
entries, including their insertion and removal from theOverlay
.
Usage
_history
field of NavigatorState
Signature:
List<_RouteEntry> _history = <_RouteEntry>[];
Core Functionality:
-
Route Stack Management: The
_history
list inNavigatorState
serves as the stack that holds the routes managed by theNavigator
. Each item in this list is an instance of_RouteEntry
, which, as previously discussed, represents aRoute
along with its lifecycle state and overlay entries. -
Order and Navigation Logic: The order of the
_RouteEntry
objects in the_history
list defines the navigation stack. The last item in the list represents the topmost route in the Navigator, which is the currently visible route if the app is running. -
Transition and State Tracking: As routes are pushed and popped from the Navigator,
_RouteEntry
objects are added to or removed from the_history
. This process is accompanied by the appropriate state transitions and animations for each route, as dictated by their respective_RouteEntry
instances. -
Lifecycle Management:
_history
helps manage the lifecycle of routes. When a new route is pushed, a new_RouteEntry
is created and added to this list. When a route is popped, its corresponding_RouteEntry
is removed, and the route undergoes its exit animation and eventual disposal.
Usage in NavigatorState
:
-
Navigation Operations: Operations like
push
,pop
,replace
, and others modify the_history
list to reflect the changes in the navigation stack. These operations handle the appropriate animations and transitions for the routes. -
Restoration and State Saving: With
NavigatorState
also mixing inRestorationMixin
, the_history
plays a role in state restoration, helping to restore the navigation stack to its previous state when the app is restarted. -
Overlay Integration: The
_overlayKey
inNavigatorState
is used to manage the overlay entries for the routes. As routes are added to or removed from_history
, their associated overlay entries are also managed accordingly.
本文作者:Maeiee
本文链接:Flutter Navigator _RouteEntry
版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!
喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!